package main

import (
	"fmt"
	"net/http"
	"reflect"
	"sync"
)

var errNum, successNum int32

func RequestGet(wg *sync.WaitGroup) {
	_, err := http.Get("http://localhost:8021/u/v1/user/list?name=1&user_id=2&passwd=123")
	if err != nil {
		// fmt.Println("error")
		errNum += 1
		wg.Done()
		return
	}
	successNum += 1
	wg.Done()
}

func SyncRequest() {
	wg := &sync.WaitGroup{}
	for i := 0; i < 10000; i++ {
		wg.Add(1)
		go RequestGet(wg)
	}
	wg.Wait()
	fmt.Printf("成功：%d, 失败：%d", successNum, errNum)
}

func jh(a, b **int) {
	*a, *b = *b, *a
}

func jhTry() {
	var a, b *int
	c, d := 1, 2
	a = &c
	b = &d
	jh(&a, &b)
	fmt.Println(*a, *b)
	fmt.Println(c, d)
}

type Person struct {
	Name   string `json:"name" validate:"required" error:"名字不能为空"`
	Age    int    `json:"age" validate:"gte=0,lte=120"`
	Height int    `json:"height,omitempty"`
}

func fsTry() {
	person := &Person{Name: "Alice", Age: 25, Height: 165}

	// 将结构体类型转换为 interface{}
	personInterface := interface{}(person)

	// 使用反射获取结构体的反射值
	personValue := reflect.ValueOf(personInterface)
	personValue1 := reflect.TypeOf(personInterface)
	if personValue1.Kind() == reflect.Ptr {
		personValue1 = personValue1.Elem()
	}
	v, _ := personValue1.FieldByName("Name")
	fmt.Println(v.Tag.Get("error"))
	// 确保 personValue 的类型是结构体
	if personValue.Kind() == reflect.Struct {
		// 遍历结构体字段
		for i := 0; i < personValue.NumField(); i++ {
			field := personValue.Type().Field(i)
			tag := field.Tag.Get("json")

			fmt.Printf("Field: %s, Tag: %s\n", field.Name, tag)
		}
	}

	// 输出：
	// Field: Name, Tag: name
	// Field: Age, Tag: age
	// Field: Height, Tag: height,omitempty
}
func main() {
	SyncRequest()
}
